Get lizards data

lizards <- read_csv(here("data_tidy", "lizards.csv"))
## Rows: 1628 Columns: 16
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (10): date, scientific_name, common_name, zone, site, plot, spp, sex, rc...
## dbl  (6): pit, toe_num, sv_length, total_length, weight, pc
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Histogram of lizard weights

ggplot(data = lizards, aes(x = weight)) + 
  geom_histogram(fill = "orange",
                 color = "purple",
                 size = 0.2)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

total_length on x-axis

#weight on y axis

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(shape = 22,
             fill = "yellow",
             color = "red",
             size = 4,
             alpha = 0.5)

Bubble plot - changing the size of a point based on the variable

color of points based on common_name and size vares with total length

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name, size = total_length),
             shape = 22,
             ) +
  theme_light()

Facet_wrap lines things up

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name)) +
  theme_light() +
  facet_wrap(~common_name, ncol = 7)

facet_grid matches the intersections

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = common_name)) +
  facet_grid(sex ~ tail)

lizard_counts <- lizards %>% 
  group_by(common_name) %>% 
  summarize(count = n())

# Same thing
lizard_counts <- lizards %>% dplyr::count(common_name)

lizard_count_cn_tail <- lizards %>% dplyr::count(common_name, tail)
ggplot(data = lizard_counts, aes(y = fct_reorder(common_name, n), x = n)) +
  geom_col(aes(fill = common_name), show.legend = FALSE) +
  labs(x = "lizard counts")

lizard_counts_new <- lizard_counts %>% 
  mutate(common_name = fct_reorder(common_name, n))

Axis scale breaks, limits and labels

Scatterplot: total_length (x) versus weight (y)

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point() +
  scale_x_continuous(breaks = c(0, 10, 50, 500),
                     limits = c(0, 500),
                     expand = c(0, 0)) +
  scale_y_continuous(expand = c(0,0))

# Transform the date column to class Date, then find counts of observations by date.

lizard_counts <- lizards %>% 
  mutate(date = lubridate::mdy(date)) %>% 
  count(date)
  
class(lizard_counts$date)
## [1] "Date"

make a line plot (geom_line()) of date (x) and count (y)

ggplot(data = lizard_counts, aes(x= date, y = n)) +
  geom_line() +
  scale_x_date(date_breaks = "3 years",
               date_labels = "%y")

ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_point(aes(color = weight)) +
  scale_color_stepsn(colors = c("green", "purple", "blue"),
                     breaks = c(0, 20, 60))

Update a color scheme using a palette in paletteer

make a horizontal box plot with common_name on the y axis and total length on the x axis with color changing based on the common_name

lizards_fct <- lizards %>% 
  mutate(common_name = fct_reorder(common_name, total_length, .fun = median))


ggplot(data = lizards, aes(x = total_length, y = common_name)) +
  geom_boxplot(aes(fill = common_name), show.legend = FALSE) +
  scale_fill_paletteer_d(palette = "colorBlindness::LightBlue2DarkBlue7Steps")

ggplot(data = lizards, aes(x = total_length, y = common_name)) +
  geom_point() +
  theme(
    panel.background = element_rect(fill = "yellow", 
                                    color = "purple",
                                    size = 10),
    panel.grid.major.x = element_line(color = "red"),
    panel.grid.major = element_line(color = "blue"),
    panel.grid.minor.y = element_line(color = "orange")
  )

ggplot(data = lizards, aes(x = total_length, y = common_name)) +
  geom_point() +
  theme(
    panel.grid = element_blank(),
    plot.background = element_rect(fill = "cyan4"),
    axis.text.x = element_text(color = "orange"),
    axis.title = element_text(color = "yellow", size = 15))

gg repel

ww_lizards <- lizards %>% 
  filter(common_name == "western whiptail", site == "sand")

ggplot(data = ww_lizards, aes(x = total_length, y = weight)) +
  geom_point() +
  geom_text_repel(aes(label = toe_num), size = 3, max.overlaps = 20)

Filter for only Europe, then create a scatterplot of gdpPercap (x) and life expectancy

europe <- gapminder %>% 
  filter(continent == "Europe", year == "1952")

ggplot( data = europe, aes(x = gdpPercap, y = lifeExp)) +
  geom_point() +
  geom_text_repel(aes(label = country), size = 3)

gghighlight

p <- ggplot(lizards, aes(x = total_length, y = weight)) +
  geom_point()

p + gghighlight(toe_num == 250, label_key = toe_num)

line plot total lengthh vs. weight

q <- ggplot(data = lizards, aes(x = total_length, y = weight)) +
  geom_line(aes(color = common_name)) +
  gghighlight(max(weight) > 30)
## label_key: common_name
q

patchwork for compound figures

(p | q) / q &
  theme_minimal()

A few new graph types

Marginal plots

whiptails <- lizards %>% 
  filter(common_name == "western whiptail") %>% 
  drop_na(total_length, weight)

ggplot(data = whiptails, aes(x = total_length , y = weight)) +
  geom_point()

Marginal plot with box plots

my_plot <- ggplot(data = whiptails, aes(x = total_length , y = weight)) +
  geom_point(aes(color = sex), size = 2) +
  scale_color_manual(values = c("cyan4", "black", "goldenrod"),
                     name = "Sex:",
                     labels = c("female", "juvenile", "male")) +
  theme_minimal() +
  theme(legend.position = "bottom")

ggMarginal(my_plot, type = "boxplot", groupColor = TRUE)
## Warning: Ignoring unknown parameters: groupColor

## Warning: Ignoring unknown parameters: groupColor

Beeswarm plot

ggplot(data = whiptails, aes(x = sex, y = weight)) +
  geom_beeswarm() + 
  geom_violin(fill = NA) + 
  geom_boxplot(fill = NA)

heatmap with geom_tile()

make a new data frame called lizard_counts, starting from lizards, with date converted to class Date. Then count by year and common name the number of lizards observed. (i.e., the outcome should be a table with total counts of lizards observed by year and common name)

lizard_counts <- lizards %>% 
  mutate(date = lubridate::mdy(date)) %>% 
  mutate(year = lubridate::year(date)) %>% 
  count(year, common_name)


ggplot(data = lizard_counts, aes(x = year, y = common_name)) +
  geom_tile(aes(fill = n)) +
  geom_text(aes(label = n), color = "white", size = 3) +
  scale_fill_viridis_c()

Make a map

Use read_sf to read in the “doc.kml” file.

jornada_vegetation <- read_sf(here("data_raw", "spatial_vegetation", "doc.kml")) %>% 
  select(Name) %>% 
  clean_names()

ggplot(data = jornada_vegetation) +
  geom_sf(aes(fill = name), color = "white", size = 0.2) +
  scale_fill_paletteer_d(palette = "ggthemes::manyeys") +
  labs(x= "Longitude",
       y = "Latitude",
       fill = "dominant vegetation:")